package com.example.tatil.controller;

import com.example.tatil.pojo.Dept;
import com.example.tatil.pojo.Result;
import com.example.tatil.service.DeptService;
import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Autowired;



import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


//部门管理Controller
@Slf4j //日志对象简化注解 lombok提供
@RestController
@RequestMapping("/depts")
public class DeptController {

    @Autowired//注入
    private DeptService deptService;
//    private  static Logger log = LoggerFactory.getLogger(DeptController.class);
    //查询部门信息
//    @RequestMapping(value = "/depts",method = RequestMethod.GET)//指定为只能Get方式访问
    @GetMapping()//限定访问方式为get上面的简化
    public Result list(){
        log.info("查询全部部门数据");
        //利用services来查询部门数据
        List<Dept> deptlist = deptService.list();
//
        return Result.success(deptlist);
    }

    //删除部门
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) throws Exception {
        log.info("根据id删除部门:{}",id);
        deptService.delete(id);
        return Result.success();
    }

    //添加部门
    @PostMapping()
    public Result add(@RequestBody Dept dept){
        log.info("添加部门:{}",dept);
        //利用service曾来添加部门
        deptService.add(dept);
        return Result.success();


    }

    //根据id查询部门数据
    @GetMapping("/{id}")
    public Result selec(@PathVariable Integer id){
        //利用service来查询部门
        log.info("根据id查询部门信息：{}",id);
        Dept dept = deptService.select(id);
        return Result.success(dept);
    }

    //修改部门
    @PutMapping
    public Result updateDept(@RequestBody Dept dept){

        try {
            log.info("根据id修改部门名称修改信息:{}",dept);
            deptService.updateDept(dept);
            return Result.success();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

}
